01. Spring Boot Template Engine

In the previous lesson, we have introduced microservice, MVC framework and Spring Boot framework. I believe now you have a good sense about how to create a simple Spring Boot application. Let’s continue our Spring Boot journey. In this lesson we are going to learn the Spring Boot template engine and a persistence framework. There are three major template engines in Spring Boot which are:

  1. Freemarker
  2. JSP
  3. Thymeleaf
    We are going to focus on Thymeleaf for this course. But I will still give you a brief introduction on Freemarker. I won’t cover JSP, because it is not widely used in spring boot application.

035ND C01 L03 A01 WELCOME

Freemarker Introduction

FreeMarker is a Java based template engine package from Apache.

Actually I was one of the contributors for that project. It’s become a fully accepted Apache project in 2018.

FreeMarker work as a sequential text processor which is a different approach compared to Thymeleaf. But again, FreeMarker is a new project, and I see the potential in it. In this lesson, we are going to briefly show how to use FreeMarker in SpringBoot. If you are interested in FreeMarker, please feel free to check it out at its website: https://freemarker.apache.org/.

Instructions

(Resources: Freemaker: https://freemarker.apache.org/. )

Goto https://start.spring.io/ change artifact to freemarker

Add web-starter and freemarker as dependency.

Download, unzip and import the project.

Create a model directory and create Student class with id, studentname, and grade as our model. Make sure you set up the constructor.

public class Student {
   private Integer Id;
   private String studentName;
   private double grade;

   public Student(Integer id, String studentName, double grade) {
       Id = id;
       this.studentName = studentName;
       this.grade = grade;
   }

   public double getGrade() {
       return grade;
   }

   public void setGrade(double grade) {
       this.grade = grade;
   }

   public String getStudentName() {
       return studentName;
   }

   public void setStudentName(String studentName) {
       this.studentName = studentName;
   }

   public Integer getId() {

       return Id;
   }

   public void setId(Integer id) {
       Id = id;
   }
}

Create a controller directory and create a StudentList class as our controller.

@Controller
public class StudentList {

   @RequestMapping("/list")
   public String list(Model model) {
       List<Student> list = new ArrayList<>();
       list.add(new Student(1, "Tom", 80.5));
       list.add(new Student(2, "Jerry", 90.4));
       list.add(new Student(3, "Paul", 77.5));
       model.addAttribute("list", list);
       return "list";
   }
}

Create a list.html in resource/templates contents below

<html>
   <title>Student List</title>
   <meta charset="utf-8" />
   <body>
       <h3>Student Grade Table</h3>
       <table>
           <tr>
               <th>Student ID</th>
               <th>Student Name</th>
               <th>Grade</th>
           </tr>
           <#list list as student>
           <tr>
               <td>${student.id}</td>
               <td>${student.studentName}</td>
               <td>${student.grade}</td>
           <tr>
           </#list>
       </table>
   </body>
</html>

Start the application and goto localhost:8080/list

Spring Boot with Freemarker

035ND C01 L03 A02 SPRINGBOOT WITH FREEMARKER

Few things you might noticed during the development. We are using @Controller this time rather than @RestController. To clarify, @RestController is used to tell that the response sent from your controller should be sent to the browser, usually an object mapped to json. It is the same as adding @ResponseBody.

We are adding the view in template rather than static folder.

We are creating a student model with three attributes. And the controller send a student List back to the freemarker view. Please take a look at the freemarker’s syntax and understand how it works.